//
//  ClubDynamicDetailView.swift
//  77Club
//
//  Created by Apple on 2024/8/30.
//

import UIKit

//MARK: 动态详情 nav View
class ClubDynamicDetailNavView: UIView {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let iconIamgeView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(16).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#17181A").font(12.font_semiBold).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(10.font_medium).end()
    private let shareButton = c_make_ex(UIButton.self)
        .image("GameDetail_more").end()
    private let userButton = c_make_ex(UIButton.self).end()
    
    var backBlock: (() -> Void)?
    var shareBlock: (() -> Void)?
    var userBlock: ((_ uid: String) -> Void)?
    
    var isRemove = false{
        didSet{
            iconIamgeView.isHidden = isRemove
            nameLabel.isHidden = isRemove
            subLabel.isHidden = isRemove
            shareButton.isHidden = isRemove
        }
    }
    
    var model: ClubGameCircleModel?{
        didSet{
            guard let md = model else { return }
            iconIamgeView.loadImage(url: md.face)
            nameLabel.text = md.pet_name
            subLabel.text = md.time + "·" + md.look + "浏览"
        }
    }
    
    var forumModel: ClubGameCommunityDetailModel?{
        didSet{
            guard let md = forumModel else { return }
            iconIamgeView.loadImage(url: md.face)
            nameLabel.text = md.fabu
            subLabel.text = md.time + "·" + md.look + "浏览"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            backButton
            iconIamgeView
            nameLabel
            subLabel
            shareButton
            userButton
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        iconIamgeView.leading(56).size(32)
        iconIamgeView.CenterY == backButton.CenterY
        shareButton.trailing(8).size(32)
        shareButton.CenterY == backButton.CenterY
        nameLabel.leading(100).top(.statusHeight + 8).trailing(50)
        subLabel.leading(100).top(.statusHeight + 24).trailing(50)
        userButton.leading(56).height(44).width(100).top(.statusHeight)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        shareButton.addTarget(self, action: #selector(clickShareButton), for: .touchUpInside)
        userButton.addTarget(self, action: #selector(clickUserButton), for: .touchUpInside)
    }
    
    @objc private func clickBackButton(){
        backBlock?()
    }
    
    @objc private func clickShareButton(){
        shareBlock?()
    }
    
    @objc private func clickUserButton(){
        if let uid = model?.uid_des {
            userBlock?(uid)
        }
        if let uid = forumModel?.uid_des {
            userBlock?(uid)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}


//MARK: 动态详情bottom View
class ClubDynamicDetailBottomView: UIView, RaeNetworkProtocol {

    private let lineView = c_make_ex(UIView.self)
        .bgColor("#E4E5E6").end()
    private let textButton = c_make_ex(UIButton.self)
        .bgColor(.black.alpha(0.05)).corner(17)
        .text("    写点什么...").color("#7B7D80")
        .font(13.font_medium).end()
    let likeButton = UIIconButton(style: .top(itemSpace: 2))
    let collectButton = UIIconButton(style: .top(itemSpace: 2))
    private let commitButton = UIIconButton(style: .top(itemSpace: 2))
    private let shareButton = UIIconButton(style: .top(itemSpace: 2))
    
    var likeBlock: (() -> Void)?
    var commitBlock: (() -> Void)?
    var shareBlock: (() -> Void)?
    var collectBlock: (() -> Void)?
    
    var model: ClubGameCircleModel?{
        didSet{
            guard let md = model else { return }
            shareButton.isHidden = false
            collectButton.isHidden = true
            collectButton.widthConstraint?.constant = 0
            likeButton.isSelected = md.is_zan
            if md.zan != "0" {
                likeButton.setTitle("\(md.zan)", for: .normal)
            }else{
                likeButton.setTitle("点赞", for: .normal)
            }
            if md.pinglun != "0" {
                commitButton.setTitle("\(md.pinglun)", for: .normal)
            }else{
                commitButton.setTitle("评论", for: .normal)
            }
            textButton.widthConstraint?.constant = 195.0.Scale
        }
    }
    
    var forumModel:ClubGameCommunityDetailModel?{
        didSet{
            guard let md = forumModel else { return }
            shareButton.isHidden = false
            collectButton.isHidden = false
            collectButton.widthConstraint?.constant = 44
            collectButton.isSelected = md.is_shoucang == 1
            likeButton.isSelected = md.is_good
            if md.good != "0" {
                likeButton.setTitle("\(md.good)", for: .normal)
            }else{
                likeButton.setTitle("点赞", for: .normal)
            }
            if md.pinglun != "0" {
                commitButton.setTitle("\(md.pinglun)", for: .normal)
            }else{
                commitButton.setTitle("评论", for: .normal)
            }
            textButton.widthConstraint?.constant = 195.0.Scale
        }
    }
    
    var commitModel: ClubGameDetailCommentListModel?{
        didSet{
            guard let md = commitModel else { return }
            shareButton.isHidden = true
            collectButton.isHidden = false
            collectButton.widthConstraint?.constant = 44
            likeButton.isSelected = md.is_zan
            if md.zan != "0" {
                likeButton.setTitle("\(md.zan)", for: .normal)
            }else{
                likeButton.setTitle("点赞", for: .normal)
            }
            if md.num != "0" {
                commitButton.setTitle(md.num, for: .normal)
            }else{
                commitButton.setTitle("评论", for: .normal)
            }
            textButton.widthConstraint?.constant = 245.0.Scale
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        
        subviews{
            lineView
            textButton
            likeButton
            collectButton
            commitButton
            shareButton
        }
        
        lineView.leading(0).top(0).trailing(0).height(1)
        likeButton.trailing(12).top(4).size(44)
        collectButton.trailing(56).top(4).size(44)
        commitButton.top(4).size(44)
        commitButton.Trailing == collectButton.Leading
        shareButton.top(4).size(44)
        shareButton.Trailing == commitButton.Leading
        textButton.leading(12).top(9).height(34)
        textButton.Trailing == shareButton.Leading - 10.0.Scale
        
        textButton.contentHorizontalAlignment = .leading
        
        likeButton.style { v in
            v.iconSize = CGSize(width: 24, height: 24)
            v.setImage("Square_detail_like".toImage, for: .normal)
            v.setImage("Square_detail_liked".toImage, for: .selected)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#E65C6E".toColor, for: .selected)
            v.setTitle("点赞", for: .normal)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
        }
        collectButton.style { v in
            v.iconSize = CGSize(width: 24, height: 24)
            v.setImage("Home_detail_collect".toImage, for: .normal)
            v.setImage("Home_detail_collected".toImage, for: .selected)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .selected)
            v.setTitle("收藏", for: .normal)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(clickCollectButton), for: .touchUpInside)
        }
        commitButton.style { v in
            v.iconSize = CGSize(width: 24, height: 24)
            v.setImage("Square_detail_commit".toImage, for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.titleLabel?.font = 10.font_medium
            v.setTitle("评论", for: .normal)
            v.addTarget(self, action: #selector(clickCommitButton), for: .touchUpInside)
        }
        shareButton.style { v in
            v.iconSize = CGSize(width: 24, height: 24)
            v.setImage("Square_detail_share".toImage, for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.titleLabel?.font = 10.font_medium
            v.setTitle("分享", for: .normal)
            v.addTarget(self, action: #selector(clickShareButton), for: .touchUpInside)
        }
        textButton.addTarget(self, action: #selector(clickCommitButton), for: .touchUpInside)
    }
    
    @objc private func clickLikeButton(){
        likeBlock?()
    }
    @objc private func clickCommitButton(){
        commitBlock?()
    }
    @objc private func clickCollectButton(){
        
        if let id = forumModel?.id {
            if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                Task {
                    let res = await network.requestSpecial(.collectForum(newsId: id),isShowError: false)
                    if res.isSuccess {
                        self.collectButton.isSelected = !self.collectButton.isSelected
                        if self.collectButton.isSelected {
                            "收藏成功".toast_success
                        }else{
                            "已取消收藏".toast_success
                        }
                    }
                }
            }else{
                UIApplication.topController()?.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
            }
        }
        
    }
    @objc private func clickShareButton(){
        shareBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}


//MARK: 分区titileView
class ClubDynamicDetailHeaderView: UITableViewHeaderFooterView {
         
    private let allLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold)
        .text("全部评论").end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(12.font_semiBold).end()
    
    var titleString: String? {
        didSet{
            guard let str = titleString else { return }
            nameLabel.text = str
        }
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = "#F7F8FA".toColor
        contentView.backgroundColor = "#F7F8FA".toColor

        addSubview(allLabel)
        addSubview(nameLabel)
        
        allLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12.0.Scale)
            make.top.equalToSuperview().offset(16.0.Scale)
        }
        nameLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(69.0.Scale)
            make.top.equalToSuperview().offset(18.0.Scale)
        }
    }
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}

class ClubDynamicDetailommitView: UIView, UITextViewDelegate {

    private let sendButton = c_make_ex(UIButton.self)
        .image("Square_send").image("Square_sendDisable",state: .disabled).end()
    lazy var commitView: Custom_TextView = {
        let view = Custom_TextView()
        view.backgroundColor = "#000000".toColor.alpha(0.05)
        view.textContainerInset = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 15)
        view.tintColor = .black
        view.textColor = .black
        view.placeHolderColor = "#7B7D80".toColor
        view.placeHolder = "写点什么..."
        view.placeholderFont = 11.font_medium
        view.returnKeyType = .done
        view.font = 11.font_medium
        view.layer.cornerRadius = 16
        view.layer.masksToBounds = true
        return view
    }()
    var sendTextBlock: ((_ text: String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white.alpha(0.8)
        
        subviews{
            commitView
            sendButton
        }

        commitView.delegate = self
        commitView.leading(12.0.Scale).height(32.0.Scale).trailing(65.0.Scale).centerVertically()
        sendButton.trailing(12.0.Scale).height(32.0.Scale).width(45.0.Scale).centerVertically()
        
        sendButton.isEnabled = false
        sendButton.addTarget(self, action: #selector(sendMessage), for: .touchUpInside)
    }
    
    @objc private func sendMessage(){
        guard let text = commitView.text, text.count > 0 else { return }
        sendTextBlock?(text)
    }

    // UITextViewDelegate 方法
    func textViewDidChange(_ textView: UITextView) {
        // 获取当前字符数
        let characterCount = textView.text.count
        sendButton.isEnabled = characterCount > 0
        // 可在此处添加其他逻辑，例如限制字符长度
        if characterCount > 200 {
            textView.text = String(textView.text.prefix(200))
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//MARK:
class ClubDynamicDetailCommitEmptyCell: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_address_empty").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold)
        .alignment(.center).text("还没有评论哦~").end()
    private let commitButton = c_make_ex(UIButton.self)
        .image("Common_firstCommit").end()
    var commitBlock: (() -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            titleLabel
            commitButton
        }
        
        iconImageView.top(40).width(96.0.Scale).height(131.0.Scale).centerHorizontally()
        titleLabel.leading(20).trailing(20)
        titleLabel.Top == iconImageView.Bottom + 15.0.Scale
        commitButton.width(72.0.Scale).height(26.0.Scale).centerHorizontally().bottom(10)
        commitButton.Top == titleLabel.Bottom + 16.0.Scale
        
        commitButton.addTarget(self, action: #selector(clickCommitButton), for: .touchUpInside)
    }
    
    @objc private func clickCommitButton(){
        commitBlock?()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
